using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace BackGroundWorkerTry
{
    public partial class Form1 : Form
    {
        Search _SearchObj;
        DateTime startDate = DateTime.Now;

        public Form1()
        {
            InitializeComponent();
            _bgWorkerSearch.WorkerReportsProgress = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnChooseDir_Click(object sender, EventArgs e)
        {
            DialogResult _DR = _fbdFolderName.ShowDialog();

            if (_DR == DialogResult.Yes || _DR == DialogResult.OK)
            {
                if (ValidateDirectory(_fbdFolderName.SelectedPath))
                {
                    txtFolderName.Text = _fbdFolderName.SelectedPath;
                }

            }
        }

        private bool ValidateDirectory(string _DirectoryName)
        {
            DirectoryInfo _DirObj = new DirectoryInfo(_DirectoryName);
            
                if (_DirObj.Exists)
                    return true;
                else
                    return false;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.InitSearch();
        }

        private void InitSearch()
        {
            if (ValidateSearch())
            {
                DisableControls();

                StartSearch();
            }
        }

        private void StartSearch()
        {
            _bgWorkerSearch.RunWorkerAsync();
            _progressTimer.Start();
        }

        private void DisableControls()
        {
            
        }

        private bool ValidateSearch()
        {
            if (ValidateDirectory(txtFolderName.Text) && txtSearch.Text != String.Empty)
                return true;
            else
                return false;
        }

        private void _bgWorkerSearch_DoWork(object sender, DoWorkEventArgs e)
        {
            List<string> _matchedNames;
            lblStatus_toolStrip.Text = "Loading....thanks for your patience";
            _matchedNames = this.SearchAllFiles(txtFolderName.Text, txtSearch.Text);
            e.Result = _matchedNames;
        }

        private List<string> SearchAllFiles(string _dirName, string _searchText)
        {
            DirectoryInfo _DIObj = new DirectoryInfo(_dirName);

            List<string> AllFilesList = new List<string>();

            FileInfo[] _fiObj = _DIObj.GetFiles(_searchText);

            foreach (FileInfo _temp in _fiObj)
                AllFilesList.Add(_temp.FullName);

            return AllFilesList;
        }

        private void _bgWorkerSearch_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            toolStripProgressBar1.Value = e.ProgressPercentage;
            lblStatus_toolStrip.Text = "Loading ... " +
                e.ProgressPercentage.ToString() + "%";
        }

        private void _bgWorkerSearch_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            lbxFileNames.DataSource = e.Result;
            lblStatus_toolStrip.Text = "";
            _progressTimer.Stop();
        }

        private void _progressTimer_Tick(object sender, EventArgs e)
        {
            TimeSpan ts = DateTime.Now.Subtract(startDate);
            string sTime = "  ..." + ts.Minutes.ToString("00") +
               ":" + ts.Seconds.ToString("00") +
               ":" + ts.Milliseconds.ToString("000");
            lblStatusToolStrip.Text = sTime;
            if (toolStripProgressBar1.Value ==
                toolStripProgressBar1.Maximum)
            {
                toolStripProgressBar1.Value = 0;
            }
            toolStripProgressBar1.PerformStep();
        }
    }
}